IEntity.cs
Location: Domain Project > Framework > Interfaces
public interface IEntity<TKey>
{
public TKey Id { get; set; }
}
Entity.cs
Location: Domain Project > Framework > Base
public class Entity<TKey> : IEntity<TKey>
{
public TKey Id{ get; set; }
}
this approach is valid and commonly used in Domain-Driven Design (DDD) and Clean Architecture. It provides consistency, reusability, and common functionality across all entities.
Location: Domain Project > Aggregates > (RelatedFolder)
public class Account : Entity<long>
{
public required string PhoneNumber { get; set; }
public required string Password { set; get; }
public string? Email { get; set; }
public long? PersonId { get; set; }
}
Navigation properties in Entity Framework Core (EF Core) represent relationships between entities. They allow you to navigate (follow) the relationships between different tables using C# objects instead of writing SQL joins manually.
For example, if you have a Ticket entity related to a Buyer, the navigation property allows you to access the buyer from a ticket without writing a separate SQL query.
Navigation properties can be of two types:
Relationship Type | Description |
---|---|
Reference Navigation Property | Represents a single entity related to another (one-to-one or many-to-one) |
Collection Navigation Property | Represents a list of related entities (one-to-many or many-to-many) |
A Buyer can have multiple Tickets, but each Ticket belongs to one Buyer.
public class Buyer
{
public int Id { get; set; }
public string Name { get; set; }
// Navigation Property (One Buyer → Many Tickets)
public virtual ICollection<Ticket> Tickets { get; set; } = new List<Ticket>();
}
public class Ticket
{
public int Id { get; set; }
public int BuyerId { get; set; }
// Navigation Property (Many Tickets → One Buyer)
public virtual Buyer Buyer { get; set; }
}
A Ticket can have only one Transaction, and a Transaction belongs to exactly one Ticket.
public class Ticket
{
public int Id { get; set; }
// One-to-One Navigation Property
public virtual Transaction Transaction { get; set; }
}
public class Transaction
{
public int Id { get; set; }
public int TicketId { get; set; }
// One-to-One Navigation Property
public virtual Ticket Ticket { get; set; }
}
A Buyer can buy many Tickets, and each Ticket can be bought by many Buyers (if resale is allowed).
public class Buyer
{
public int Id { get; set; }
public string Name { get; set; }
// Many-to-Many Navigation Property
public virtual ICollection<Ticket> Tickets { get; set; } = new List<Ticket>();
}
public class Ticket
{
public int Id { get; set; }
// Many-to-Many Navigation Property
public virtual ICollection<Buyer> Buyers { get; set; } = new List<Buyer>();
}
virtual
Microsoft.EntityFrameworkCore.Proxies
to Infrastructure Project